<template>
  <div>
    <nav-bar :title="data.itemData.cardTitle" v-if="data">
      <template v-slot:right>
        <span class="fontSize14" @click="clearRead" v-if="data.active === 7 || data.active === 9 || data.active === 10 || data.active === 11">清除未读</span>
      </template>
    </nav-bar>
    <div class="messageDetails maxinBox">
      <div class="backgroundfff">
        <card-content v-for="(item, index) in dataList" :key="index" :data="item" cardType="message"></card-content>
      </div>
    </div>
  </div>
</template>

<script>
  import fixedInspectionAPI from '../../../api/workAPI/fixedInspectionAPI.js'
  import realTimeInspectionAPI from '../../../api/workAPI/realTimeInspectionAPI.js'
  import overtimePayManage from '../../../api/workAPI/overtimePayManage.js'
  import hotCardManageAPI from '../../../api/workAPI/hotCardManageAPI.js'
  import messageAPI from '../../../api/messageAPI.js'
  import CardContent from '../components/CardContent.vue'
  export default {
    components:{
      CardContent
    },
    data() {
      return {
        data: null,
        dataList: [],
        warnIds: []
      }
    },
    methods:{
      // 清除未读
      clearRead() {
        messageAPI.delRead(this.warnIds).then(res => {
          this.$toast('清除成功')
          this.dataList = []
          this.getList()
        })
      },
      // 查询消息列表
      getList() {
        let projectId = localStorage.getItem('projectId')
        let subType = this.data.active
        let type = ''
        if (subType === 1) {
          let data = {
            projectId: localStorage.getItem('projectId'),
            completed: 0
          }
          fixedInspectionAPI.getTaskList(data).then(res => {
            res.forEach(item => {
              this.dataList.push({
                itemData: item,
                cardTitle: item.patrolPlanName || '--',
                cardImg: require('../../../assets/message/message8.png'),
                cardDesc: `请于${item.execStartTime}前进行`,
                cardId: 1
              })
            })
          })
        } else if (subType === 2) {
          let data = {
            completed: 0,
            projectId: localStorage.getItem('projectId'),
            dutyType: 1
          }
          fixedInspectionAPI.getTaskAboutMyList(data).then(res => {
            res.forEach(item => {
              this.dataList.push({
                itemData: item,
                cardTitle: item.patrolPointName || '--',
                cardImg: item.reformAttachmentList[0],
                cardDesc: item.troubleDesc || '--',
                cardId: 2
              })
            })
          })
        } else if (subType === 3) {
          let data = {
            reformStatus: 1,
            projectId: localStorage.getItem('projectId'),
            dutyType: 1
          }
          realTimeInspectionAPI.getAboutMy(data).then(res => {
            res.forEach(item => {
              if (item.trouble) {
                if (item.trouble.troubleAttachmentList) {
                  this.dataList.push({
                    cardImg: item.trouble.troubleAttachmentList[0],
                    itemData: item,
                    cardTitle: item.pointDesc || '--',
                    cardDesc: item.trouble.troubleDesc || '--',
                    cardId: 3
                  })
                } else {
                  this.dataList.push({
                    cardImg: '--',
                    itemData: item,
                    cardTitle: item.pointDesc || '--',
                    cardDesc: item.trouble.troubleDesc || '--',
                    cardId: 3
                  })
                }
              }
            })
          })
        } else if (subType === 16) {
          overtimePayManage.getWorkOvertimeDataList(1).then(res => {
            res.forEach(item => {
              this.dataList.push({
                cardImg: '',
                itemData: item,
                cardTitle: `${item.applyName}的加班审批`,
                cardDesc: `${item.applyDate}提交加班审批`,
                cardId: 10
              })
            })
          })
        } else if (subType === 15) {
          hotCardManageAPI.getHotCardDataList(1).then(res => {
            res.forEach(item => {
              this.dataList.push({
                cardImg: '',
                itemData: item,
                cardTitle: `${item.applicant}的动火证审批`,
                cardDesc: `${item.applyDate}提交动火证审批`,
                cardId: 15
              })
            })
          })
        } else {
          messageAPI.getMessagelist(projectId, type, subType).then(res => {
            if (subType === 4) {
              res.ccList.forEach(item => {
                this.dataList.push({
                  itemData: item,
                  cardTitle: item.messageTitle || '--',
                  cardImg: require('../../../assets/message/message2.png'),
                  cardDesc: item.messageContent || '--',
                  cardId: 4
                })
              })
            } else if (subType === 5) {
              res.visitorList.forEach(item => {
                this.dataList.push({
                  itemData: item,
                  cardTitle: item.messageTitle || '--',
                  cardImg: require('../../../assets/message/message2.png'),
                  cardDesc: item.messageContent || '--',
                  cardId: 5
                })
              })
            } else if (subType === 7) {
              this.warnIds = res.envWarning.map(item => {
                return item.messageId
              })
              res.envWarning.forEach(item => {
                this.dataList.push({
                  cardTitle: item.messageTitle || '--',
                  cardImg: require('../../../assets/message/message7.png'),
                  cardDesc: item.messageContent || '--',
                  isButton: 1,
                  cardHot: !item.readFlag,
                  cardId: 7
                })
              })
            } else if (subType === 8) {
              res.illegal.forEach(item => {
                this.dataList.push({
                  itemData: item,
                  cardTitle: item.messageTitle || '--',
                  cardImg: require('../../../assets/message/message5.png'),
                  cardDesc: item.messageContent || '--',
                  cardHot: !item.readFlag,
                  cardId: 8
                })
              })
            } else if (subType === 9) {
              this.warnIds = res.bowOut.map(item => {
                return item.messageId
              })
              res.bowOut.forEach(item => {
                this.dataList.push({
                  itemData: item,
                  cardTitle: item.messageTitle || '--',
                  cardImg: require('../../../assets/message/message3.png'),
                  cardDesc: item.messageContent || '--',
                  isButton: 1,
                  cardHot: !item.readFlag,
                  cardId: 9
                })
              })
            } else if (subType === 11) {
              this.warnIds = res.overTimeList.map(item => {
                return item.messageId
              })
              res.overTimeList.forEach(item => {
                this.dataList.push({
                  itemData: item,
                  cardTitle: item.messageTitle || '--',
                  cardImg: require('../../../assets/message/message3.png'),
                  cardDesc: item.messageContent || '--',
                  isButton: 1,
                  cardHot: !item.readFlag,
                  cardId: 11
                })
              })
            } else if (subType === 10) {
              this.warnIds = res.hotCardList.map(item => {
                return item.messageId
              })
              res.hotCardList.forEach(item => {
                this.dataList.push({
                  itemData: item,
                  cardTitle: item.messageTitle || '--',
                  cardImg: require('../../../assets/message/message3.png'),
                  cardDesc: item.messageContent || '--',
                  isButton: 1,
                  cardHot: !item.readFlag,
                  cardId: 10
                })
              })
            }
          })
        }
      },
    },
    mounted() {
      this.data = JSON.parse(localStorage.getItem('messageDetails'))
      this.getList()
    }
  }
</script>
